# -*- codeing = utf-8 -*-
# @Time : 2022/1/16 22:57
# @Author : 苏宏航
# @File : bili_crawler.py
# @Software: PyCharm
import requests
import time
from lxml import etree
import json
import pymysql
import emoji
  # 获取页面
def get_html(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
    }
    try:
        r = requests.get(url,headers=headers)
        r.raise_for_status()  # 如果状态不是200，引发httperror异常
        r.encoding = 'utf-8'
    except:
        print('请求出错' + url)
    return r
def parse_detail(url):
    """解析详情页"""
    response = get_html(url)
    data = json.loads(response.text)
    message_list = []
    for i in data["data"]["replies"]:
        a={}
        # 评论信息
        message = i["content"]["message"]
        a["message"] = emoji.demojize(message)
        # print(message)
        # 评论人
        # 昵称
        a["uname"]=i["member"]["uname"]
        # 性别
        a["sex"] = i["member"]["sex"]
        # 点赞数
        a["likes"]= i["like"]
        message_list.append(a)
    return message_list
def save(message_list):
    # db = pymysql.connect('localhost', 'root', 'django@123', 'test')
    db = pymysql.connect(host="www.91iedu.com",port=3391,user="tjpu1904",password="tjpu1904",db="tjpu1904",charset="utf8")
    cursor = db.cursor()  # 建立游标
    # cursor.execute('create table blibli3(title varchar(255),url varchar(255),comment varchar(1000))')
    for i in message_list:
        sql = "insert into vido_comment(content,user,sex,likes) values (%s,%s,%s,%s)"
        cursor.execute(sql,(i["message"],i["uname"],i["sex"],i["likes"]))
        db.commit()  # 提交数据库
    cursor.close


if __name__ == '__main__':
    # 获取评论
    """【神盾局】男人想成为下一个钢铁侠，但很快就要被现实打脸了！
    http://www.bilibili.com/video/av635469290"""
    # url1="https://api.bilibili.com/x/v2/reply/main?jsonp=jsonp&next=0&type=1&oid=635469290&mode=3&plat=1"
    av_id = "466900244"
    """爬取详情页"""
    url1 = "https://api.bilibili.com/x/v2/reply/main?jsonp=jsonp&next=0&type=1&oid="
    url2 = "&mode=3&plat=1"
    url3 = url1 + av_id + url2
    message_list = parse_detail(url3)
    print(message_list)
    #save(message_list)